<template>
  <mt-navbar title="保存海报"> </mt-navbar>
  <view v-if="detailInfo" class="safety-150">
    <l-painter ref="painterRef" css="width: 750rpx;">
      <l-painter-view css="position: relative;width: 100%;">
        <l-painter-image :src="detailInfo.bgImg" css="width: 100%;" />
        <l-painter-view css="position: absolute;bottom: 140rpx;left: 275rpx;width: 200rpx; height: 200rpx;background: #ffffff;">
          <l-painter-qrcode :text="detailInfo.qrcodeText" css="width: 180rpx; height: 180rpx;margin: 10rpx;" />
        </l-painter-view>
      </l-painter-view>
    </l-painter>

    <view class="flex items-center justify-between px-32rpx fixed h-116rpx left-0 right-0 bottom-0 bg-#ffffff safety-2">
      <mt-button class="flex-1" :shake="false" @click="handleSave"> 保存到相册 </mt-button>
    </view>
  </view>
</template>

<script setup>
const painterRef = ref();

function handleSave() {
  uni.showLoading({
    title: '正在下载',
    mask: true
  });
  painterRef.value.canvasToTempFilePathSync({
    fileType: 'png',
    pathType: 'url',
    quality: 1,
    success: (res) => {
      uni.saveImageToPhotosAlbum({
        filePath: res.tempFilePath,
        success: () => {
          uni.hideLoading();
          uni.showToast({
            icon: 'none',
            title: '保存成功'
          });
        },
        fail: () => {
          uni.hideLoading();
        }
      });
    },
    fail: () => {
      uni.hideLoading();
    }
  });
}

const detailInfo = ref(null);
onLoad(() => {
  const storage = uni.getStorageSync('poster');
  if (storage) {
    detailInfo.value = storage;
  }
});
</script>

<style lang="scss" scoped></style>
